/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRendererState;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;

/**
 *
 * @author SG0212059
 */
public class ExtendedXYPlot extends XYPlot {

    private int org = 0;
    private int top = org;

  
    public ExtendedXYPlot(XYDataset dataset, NumberAxis xAxis, NumberAxis yAxis, XYItemRenderer renderer) {
        super(dataset, xAxis, yAxis, renderer);
    }

    public void setSeriesToTop(int dataset) {
        this.top = dataset;
    }

    @Override
    public boolean render(Graphics2D g2,
            Rectangle2D dataArea,
            int index,
            PlotRenderingInfo info,
            CrosshairState crosshairState) {

       
        boolean foundData = false;

        if(index == org) {
            index = top;
        } else if(index == top) {
            index = org;
        }

        

        XYDataset dataset = getDataset(index);
        if (!DatasetUtilities.isEmptyOrNull(dataset)) {
            foundData = true;
            ValueAxis xAxis = getDomainAxisForDataset(index);
            ValueAxis yAxis = getRangeAxisForDataset(index);
            XYItemRenderer renderer = getRenderer(index);
            if (renderer == null) {
                renderer = getRenderer();
                if (renderer == null) {
                    return foundData;
                }
            }

            XYItemRendererState state = renderer.initialise(g2, dataArea, this,
                    dataset, info);
            int passCount = renderer.getPassCount();
            for (int pass = 0; pass < passCount; pass++) {
                int seriesCount = dataset.getSeriesCount();
                for (int series = 0; series < seriesCount; series++) {
                    int itemCount = dataset.getItemCount(series);
                    for (int item = 0; item < itemCount; item++) {
                        renderer.drawItem(g2, state, dataArea, info,
                                this, xAxis, yAxis, dataset, series, item,
                                crosshairState, pass);
                    }
                }
            }
        }
        return foundData;
    }
}
